import styles from './index.module.scss';
import { NavBar, TextArea, Toast } from 'antd-mobile';
import { useState } from 'react';
import { getCommentList, sendCommentByIdAction } from '@/store/action/comment';
import { useDispatch } from 'react-redux';
import { useAppSelector } from '@/store';
type Props = {
  // 评论的作者的名字
  name?: string;
  onBack: () => void;
};
export default function CommentInput({ name, onBack }: Props) {
  const [content, setContent] = useState('');
  const { detail } = useAppSelector((state) => state.article);
  const { art_id } = detail;
  const dispatch = useDispatch();
  const handlePublish = async () => {
    await dispatch(sendCommentByIdAction({ content: content, target: art_id }));
    dispatch(getCommentList({ type: 'a', source: art_id }));
    onBack();
    Toast.show({ content: '发表成功' });
  };
  return (
    <div className={styles.root}>
      <NavBar
        right={
          <span className="publish" onClick={handlePublish}>
            发表
          </span>
        }
        onBack={onBack}
      >
        {name ? '回复评论' : '评论文章'}
      </NavBar>
      <div className="input-area">
        {/* 回复别人的评论时显示：@某某 */}
        {name && <div className="at">@{name}:</div>}

        {/* 评论内容输入框 */}
        <TextArea
          placeholder="说点什么~"
          rows={10}
          value={content}
          // 这是封装的
          onChange={setContent}
        />
      </div>
    </div>
  );
}
